<?php
include 'AdminController.php';
class Admin_StactaticsController extends Admin_AdminController
{
	public function preDispatch(){
		//set layout
        $layoutPath = APPLICATION_PATH . '/templates/admin/admin';
        $option = array('layout' => 'index', 'layoutPath' => $layoutPath);
        Zend_Layout::startMvc($option);
        
        //アクティブメニューを設定
        $this->view->active_menu = "stats";
	}
	
    public function indexAction ()
    {
    	//TODO 必要なクラスを読み込む
    	Zend_Loader::loadClass("StactaticsModel");
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	//TODO: パラメータを取得
    	$start_time = $request->getParam("start_time");
    	$end_time = $request->getParam("end_time", date("Y-m"));
    	
    	if(!$start_time){
    		$start_time = $this->addMonths($end_time, -2);
    	}
    	
    	//TODO: 統計モデルのインスタンスを作成
    	$model = new StactaticsModel();
    	$stactatics_info = array();
    	
    	//TODO 統計する月を数える
    	$diff_months= date("m", strtotime($end_time) - strtotime($start_time));
    	
    	//get stactatic data for each month in range
    	for($i=0 ;$i < $diff_months; $i++){
    		$iter = $this->addMonths($start_time, $i);
    		$dt_arr = $this->getYearAndMonth($iter);
    		
    		$data = $model->getStactaticsInMonth($dt_arr['year'], $dt_arr['month']);
    		$stactatics_info[$iter] = $data;
    	}

    	//TODO 統計データに基づいてグラフのデータを作成
    	$graph_data = $this->getGraphData($stactatics_info);
    	
    	//TODO: 必要データをビューに送る
    	$view->start_time = $start_time;
    	$view->end_time = $end_time;
    	$view->graph_data = $graph_data;
    	$view->stactatics_info = $stactatics_info;
    	
    	//TODO Navigator を設定
    	$nav['ホーム'] = "/admin";
    	$nav['統計'] = "/admin/stactatics";
    	$view->nav = $nav;
    }
    
    /**
     * デートのStringから年と月を取得するためのファクション
     * @param $date_str
     */
    function getYearAndMonth($date_str){
    	$arr['year'] = substr($date_str, 0, 4);
    	$arr['month'] = substr($date_str,5);
    	
    	return $arr;
    }
    
    /**
     * 特定月にいくつかの月を追加するためのファクション
     * @param $date_str
     * @param $val
     */
    function addMonths($date_str, $val = 1){
    	if($val > 0)
    		$time = strtotime ( "+ $val months", strtotime ($date_str)) ;
    	else 
    		$time = strtotime ( "- " . (-$val) . " months", strtotime ($date_str)) ;
    	$time = date ( 'Y-m' , $time);
    	
    	return $time;
    }
    
    /**
     * 統計情報に基づいてグラフデータを作成するためのファクション
     * @param $stactatics_info
     */
   function getGraphData($stactatics_info){
   		//TODO: Generate category
   		$categories = "[";
   		foreach ($stactatics_info as $key => $value){
			if($categories != "[")
				$categories .= ",";
				
			$categories .= "'$key'";
   		}
   		$categories .= "]";
   	
   		//TODO: Generate data series
   		//会議ブースデータ
   		$total_money_series = "[";
   		$total_fee_series = "[";
   		$total_series = "[";
   		
   		foreach ($stactatics_info as $key => $value){
   			if($total_money_series != "[")
   				$total_money_series .= ",";
   			@$total_money_series .= $value['total_money'];
   				
   			if($total_fee_series != "[")
   				$total_fee_series .= ",";
   			@$total_fee_series .= $value['total_fee'];
   				
   			if($total_series != "[")
   				$total_series .= ",";
   			@$total_series .= ($value['total_money'] + $value['total_fee']);
   		}
   		
   		$total_money_series .= "]";
   		$total_fee_series .= "]";
   		$total_series .= "]";
   		
   		
   		$graph_data = array(
   			'categories' => $categories,
   			'total_money' => $total_money_series,
   			'total_fee' => $total_fee_series,
   			'total' => $total_series
   		);
   		
   		return $graph_data;
   }
}